package com.esiran.payadmin.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.esiran.payadmin.entity.Resp;
import com.esiran.payadmin.exception.APIRespException;
import com.esiran.payadmin.model.User;
import com.esiran.payadmin.model.UserCategory;
import com.esiran.payadmin.service.UserCategoryService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/user/goods/category")
public class UserCategoryController {
    private final UserCategoryService userCategoryService;
    @Autowired
    public UserCategoryController(UserCategoryService userCategoryService) {
        this.userCategoryService = userCategoryService;
    }

    @GetMapping()
    public String index(
            @RequestParam(value = "page",required = false) Integer pageNo,
            ModelMap modelMap) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Page<UserCategory> pageData = userCategoryService.getUserCategoryListById(
                user.getId(),new Page<>(pageNo==null?1:pageNo,10));
        modelMap.addAttribute("pageData",pageData);
        return "user/goods/category/index";
    }

    @GetMapping("/create")
    public String create() {
        return "user/goods/category/create";
    }

    @PostMapping("/create")
    public String createForm(UserCategory userCategory) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        userCategory.setOwnerId(user.getId());
        try {
            userCategoryService.create(userCategory);
        }catch (APIRespException e){
            return "redirect:/user/goods/category/create?err=".
                    concat(String.valueOf(e.getCode()));
        }
        return "redirect:/user/goods/category";
    }

    @GetMapping("/{id}")
    public String index(@PathVariable String id,ModelMap modelMap) {
        UserCategory uc = userCategoryService.selectById(id);
        if (uc == null){
            return "redirect:/user/goods/category?err=10008";
        }
        modelMap.addAttribute("item",uc);
        return "user/goods/category/cat";
    }

    @PostMapping("/{id}")
    public String update(UserCategory uc, @PathVariable Long id) {
        try {
            userCategoryService.updateById(uc);
        }catch (Exception e){
            return "redirect:/user/goods/category/".concat(id.toString()).concat("?err=10009");
        }
        return "redirect:/user/goods/category/";
    }

    @PostMapping("/del")
    public String del(
            @RequestParam("id") Long id) throws IOException {
        try {
            userCategoryService.deleteById(id);
        }catch (Exception e){
            return "redirect:/user/goods/category?err=10009";
        }
        return "redirect:/user/goods/category/";
    }
}
